package com.group11.studentm.view;

import com.group11.studentm.domain.StuSelectScoreS;
import com.group11.studentm.service.StudentService;
import com.group11.studentm.utils.ValidationUtil;

import java.util.List;
import java.util.Scanner;

/**
 * @date 2022/12/06/17:20
 * @since 17
 */
public class ViewStudent {
    StudentService studentService = new StudentService();
    Scanner scanner = new Scanner(System.in);

    /**
     * 学生主界面
     *
     * @param sno   学号
     * @param sname 姓名
     */

    public void mainView(String sno, String sname) {
        while (true) {
            System.out.println("---------------学生---------------");
            System.out.print("查询个人信息[1]  查询本学年成绩[2] \n修改密码[3]     退出登录[0]");
            int option = scanner.nextInt();
            switch (option) {
                case 1:// 查询个人信息
                    String sdept = studentService.findSdept(sno);
                    System.out.println("学号：" + sno + " 姓名：" + sname + " 部门：" + sdept);
                    break;
                case 2: // 查询成绩
                    //count为数据库查询的记录数
                    List<StuSelectScoreS> list = studentService.findGrade(sno);
                    int size = list.size();
                    int count = 0;  // 选修门数
                    double sum = 0; // 总分

                    System.out.println("----------学生成绩信息---------");
                    System.out.println("+------+-----------+--------+");
                    System.out.println("|课程编号|  课程名称  | 课程得分 |");
                    System.out.println("+------+-----------+--------+");
                    if (size == 0) {
                        System.out.println("| 无记录 |  无记录   |  无记录  |");
                        System.out.println("+------+-----------+--------+");
                    } else {
                        for (StuSelectScoreS stu : list) {
                            System.out.printf("|  %-4d|  %-7s|   %-5d|\n", stu.getCno(), stu.getCname(), stu.getGrade());
                            System.out.println("+------+-----------+--------+");
                            sum += stu.getGrade();  // 分数求总分
                            count++; // 选修几门
                        }
                        double average = sum / count;
                        System.out.println("总选修课程" + count + "门" + " " + "课程总分：" + sum + " " + "课程平均分：" + average);
                    }
                    break;
                case 3:  // 修改密码

                    System.out.print("旧密码：");
                    String oldPassword = scanner.next().trim(); // 旧密码,去除前后空格
                    System.out.print("新密码：");
                    String newPassword = scanner.next().trim();  // 新密码

                    // 校验旧密码，保存新密码
                    if (oldPassword.length() == 0 || newPassword.length() == 0) {
                        System.out.println("新旧密码不允许为空");
                    } else if (!ValidationUtil.checkPassword(oldPassword) || !ValidationUtil.checkPassword(newPassword)) {
                        System.out.println("密码格式不正确");
                    } else if (!studentService.findPwd(sno).equals(oldPassword)) {
                        System.out.println("输入的旧密码不正确");
                    } else if (oldPassword.equals(newPassword)) {
                        System.out.println("新旧密码一样");
                    } else {
                        if (studentService.updatePwd(newPassword, sno)) {
                            System.out.println("修改成功,即将重新登录");
                            return;  // 返回登录界面
                        } else {
                            System.out.println("抱歉，密码更新失败！");
                        }
                    }
                    break;
                case 0:
                    System.out.println("退出...");
                    return;
                default:
                    System.out.println("输入有误，重新输入");
                    break;
            }

        }


    }
}
